package com.briup.MR.Merge.map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.GzipCodec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

/**
 *准备工作：对join 操作的文件排序压缩
 * yarn jar HdfsClientDemo-1.0-SNAPSHOT.jar com.briup.MR.Merge.map.FirstStage -D input=/user/hdfs/artist.txt -D output=/user/hdfs/one
 * yarn jar HdfsClientDemo-1.0-SNAPSHOT.jar com.briup.MR.Merge.map.FirstStage -D input=/user/hdfs/user_artist.txt -D output=/user/hdfs/two
 */
public class FirstStage
        extends Configured implements Tool {
    static class FirstStageMapper
            extends Mapper<LongWritable, Text, Text, Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String [] strs=value.toString().split(",");
            context.write(new Text(strs[0]),value);
        }
    }
    /*
    map->
     key                 value
     00001               [00001,Moon Light,1973]
     00002               [00002,Scarborough Fair,1970]
     ->reduce
     */
    static class FirstStageReduce
            extends Reducer<Text,Text, NullWritable,Text> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            for (Text val:values){
                context.write(NullWritable.get(),val);
            }
        }
    }
    public int run(String[] args) throws Exception {
        Configuration conf=getConf();
        String input=conf.get("input");
        String output=conf.get("output");

        Job job= Job.getInstance(conf);
        job.setJarByClass(this.getClass());
        job.setJobName("sortflat");

        job.setMapperClass(FirstStageMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setReducerClass(FirstStageReduce.class);
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(Text.class);


        TextInputFormat.addInputPath(job,
                new Path(input));
        TextOutputFormat.setOutputPath(job,
                new Path(output));
        TextOutputFormat.setCompressOutput(job,true);
        TextOutputFormat.setOutputCompressorClass(job, GzipCodec.class);
        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(
                new ToolRunner().run(
                        new FirstStage(),args));
    }
}
